<?php
	/*
	Description: This file is used to add new venue.
	*/
	#########IncludeFiles####################################################################################
	include_once '../inc/config.php';
	include_once("../inc/bootstrape.php");
    require_once _SITE_PATH_.'classes/Venue/Venue.php';
	require_once _SITE_PATH_.'classes/Vendor/Vendor.php';
	require_once _SITE_PATH_.'classes/VendorsMaster/VendorsMaster.php';
	require_once _SITE_PATH_.'classes/CommonUtility/CommonUtility.php';
    require_once _SITE_PATH_.'classes/Master/Master.php';
		
	$id = trim($_REQUEST['id']);
	
	if(isset($_POST['submit'])){
		$params=array();
		
		$params['id'] = $_POST['id'];
		
		$params['vendor_id'] = $_POST['vendor_id'];
		$params['vendor_services'] = @implode(',',$_POST['vendor_services']);

		$params['venue_name'] = $_POST['venue_name'];
		$params['display_name'] = $_POST['display_name'];
		$params['venue_description'] = $_POST['venue_description'];
		$params['min_capacity'] = $_POST['min_capacity'];
		
		$params['max_capacity'] = $_POST['max_capacity'];
		$params['available_seating'] = $_POST['available_seating'];
		$params['address'] = $_POST['address'];
		$params['country'] = $_POST['country'];
		$params['state'] = $_POST['state'];
		
		$params['city'] = $_POST['city'];
		$params['landmark'] = $_POST['landmark'];
		$params['contact_person'] = $_POST['contact_person'];
		$params['booking_mode'] = $_POST['booking_mode'];
		
		$params['parking'] = $_POST['parking'];
		$params['venue_type'] = $_POST['venue_type'];
		$params['cutoff_days'] = $_POST['cutoff_days'];
		$params['tags'] = $_POST['tags'];
		
		$params['food_type'] = $_POST['food_type'];
		$params['zone'] = $_POST['zone'];
		$params['mobile_no'] = $_POST['mobile_no'];
		$params['landline_phone1'] = $_POST['landline_phone1'];
		$params['landline_phone2'] = $_POST['landline_phone2'];
		
		$params['latitude'] = $_POST['latitude'];
		$params['longitude'] = $_POST['longitude'];
		$params['booking_amount'] = $_POST['booking_amount'];

		$params['cancellation_policy'] = $_POST['cancellation_policy'];
		$params['tc_policy'] = $_POST['tc_policy'];
		
		if(!empty($_POST['similar_venues'])){
			$str_similar_venues = implode(',',$_POST['similar_venues']);
			$params['similar_venues'] = $str_similar_venues;
		}
		
		//$params['venue_package'] = $_POST['venue_package'];
		$params['venue_distance'] = $_POST['venue_distance'];    // upload and create thumb of the image //

	    if( !empty( $_FILES['venue_image'] ) ){
	        $filename  = 'venue_image';     //  this is name of the control you are using in html form         
	        $module='venue_images';
	
	        $uploadedfile = CommonUtility::uploadNcreateThumb($filename, $module, true, 100, true);
	        
	        if( $uploadedfile!= -1 )    {
	            $params['venue_image'] = $uploadedfile;//$_POST['package_image'];
	        }        
	    }
		
		$condition['id'] =$params['id'];
		if(CommonUtility::updateData($params,'venues',$condition)){
			$venue_id = $params['id'];
			
			// upload and create thumb of the image //
			if( !empty($_FILES['venue_images'] ) ){
				// first get all the venue images from site_galleries then use the following code within loop
            	//CommonUtility::removeImage($result['immage'], $module); 

				$parms_venue_images = array();
				$filename  = 'venue_images';     //  this is name of the control you are using in html form    
				$uploadedfile = CommonUtility::uploadNcreateThumb($filename, $module='venue_images', true, 100, true);
				if( $uploadedfile != -1 )    {
					$parms_venue_images['entity_type'] = '2';
					$parms_venue_images['entity_id'] = $venue_id;
					
					//$condition_venue_image['entity_id'] = $venue_id;
					//$condition_venue_image['entity_type'] = '2';
					//CommonUtility::deleteData('site_galleries', $condition_venue_image);					
					$i = 0;
					foreach($uploadedfile as $key => $val){
						$parms_venue_images['image'] = $val;
						$parms_venue_images['image_title'] = $_POST['image_title'][$i];
                    	$parms_venue_images['image_tooltip'] = $_POST['image_tooltip'][$i];
                    	$parms_venue_images['image_description'] = $_POST['image_description'][$i];
						$parms_venue_images['added'] = date("Y-m-d");
						CommonUtility::insertData($parms_venue_images,'site_galleries');
						$i++;
					}
				}
			}

			if(!empty($_POST['cbo_cities'])){
				$i = 0;
	            $params_cities_distance = array();
	            $params_cities_distance['venue_id'] = $venue_id;

	            CommonUtility::deleteData('venue_distances',$params_cities_distance);

	            foreach($_POST['cbo_cities'] as $key => $val){
	                $params_cities_distance['city_id'] = $val;
	                $params_cities_distance['distance'] = $_POST['distance'][$i];
	                $params_cities_distance['added'] = date("Y-m-d H:i:s");
	                CommonUtility::insertData($params_cities_distance, 'venue_distances');
	                $i++;
	            } 
	        }
			
			header("Location: venue_mng.php?msg=1336"); 
			exit;			
		}
		$err = 1337;
	}

	$editor = CommonUtility::includeEditor();
	
	$venue_details = array();
	$venue_details = Venue::getVenues($id);
	$result = $venue_details[0];
	$venue_distances = Master::getVenueDistancesByVenueId($result['id']);
	
	$services_name = '';
	if(!empty($result['vendor_services'])){
	    $vendor_services =VendorsMaster::getVendorService($result['vendor_services']);
	    $vendor_services_name = array();
	    foreach($vendor_services as $key => $val){
	        array_push($vendor_services_name, $val['name']);
	    }
	    $services_name = implode(',',$vendor_services_name);
	}

	$sel_vendor_services = @explode(',',$result['vendor_services']);
	$vendor_services = CommonUtility::getVendorServicesByVendorId($result['vendor_id'],$sel_vendor_services);

	$selected_cities = CommonUtility::getCitiesForState($result['state']);
	$selected_cities_count = count($selected_cities);

	$entity_type = 2;
	$venue_images = CommonUtility::getCommonImages($result['id'],$entity_type);
	
	if( $errMsg ){
		$result = $_POST;    
	}else{
	  	$result['inclusion'] =  @explode(',',$result['inclusion']);
	  	$result['exclusion'] =  @explode(',',$result['exclusion']);
	}
    
	$vendors = array();
	$vendors = Vendor::getVendor();
	
	$venue_type = array();
	$venue_type = Venue::getActiveVenueTypes();
	
	$venue_services = array();
	$venue_services = Venue::getActiveVenueServices();
	
	$venue_sel_services = Venue::getSelectedVenueServices($id);
			
	$venues = array();
	$venues = Venue::getActiveVenues();
	
	$dropdown_zones = "";
	$dropdown_zones = CommonUtility::getZone($result['zone']);
	
	$dropdown_countries = array();
	$dropdown_countries = CommonUtility::getCountry($result['country']);
	
	$dropdown_states = array();
	$dropdown_states = CommonUtility::getState($result['country'],$result['state']);
	
	$dropdown_cities = array();
	$dropdown_cities = CommonUtility::getCity($result['state'],$result['city']);
	
	$booking_mode = array(1=>"online",2=>"offline",3=>"both");
	
	$food_type = array("veg"=>"Veg","nonveg"=>"Non Veg","both"=>"Both");
	
	$errMsg = $err;
	$_tplName = 'venue/venue_edit.tpl';
	$_title = 'Edit Venue';
	require_once _ADMIN_TPL_MAIN_;
?>